package de.pmg_grandma;

import interfaces.AbstractWish;
import model.Model;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import enums.State;
import enums.WishType;

public class SleeproomActivity extends Activity {

	SensorManager manager;
	public static SleeproomActivity sleepRoomActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Model.getInstance().inAppFocus = true;
		setContentView(R.layout.activity_sleeproom);

		manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

		//registerListner(welchen listner, welchen Sensor, Abfragetakt)
		manager.registerListener(listener, manager.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_NORMAL);


		ImageView tempImage = (ImageView) findViewById(R.id.bedroom);
		if (Model.getInstance().getState() == State.Awake) {
			tempImage.setImageResource(R.drawable.bedroom_disable);
		}
		tempImage.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					int x = (int) event.getX();
					int einhalb = v.getWidth()/2;
					if(x <= einhalb) {

						AbstractWish wish = Model.getInstance().getWishes().get(WishType.WishToSleep);
						if(wish != null) {
							if (!wish.canBeFulfilled()) {
								makeSleepRoomToast("KEIN SAUBERES NACHTHEMND ODER KLEID!! WASCHE WAESCHE!!");
							}
							else {
								wish.fulfill();
								//Oma schlaeft!
								MainActivity.mainActivity.sleep();
								//Oma ins Bett legen
								((ImageView)findViewById(R.id.bedroom)).setImageResource(R.drawable.bedroom_disable);
							}
						}
						else {
							if (Model.getInstance().getCLothes().contains(Model.KLEIDUNG_KLEID, 1) &&
									Model.getInstance().getCLothes().contains(Model.KLEIDUNG_NACHTHEMD, 1)) {
								//Oma schlaeft!
								MainActivity.mainActivity.sleep();
								//Oma ins Bett legen
								((ImageView)findViewById(R.id.bedroom)).setImageResource(R.drawable.bedroom_disable);
								Model.getInstance().sleep(Model.SCHLAFEN_KURZ);
							}
							else {
								makeSleepRoomToast("KEIN SAUBERES NACHTHEMND ODER KLEID!! WASCHE WAESCHE!!");
							}
						}
					}
					else {
						//Auf Kleiderschrank geklickt
						startActivityGardrobe();
					}
				}
				return true;
			}
		});
	}

	//L I C H T S E N S O R
	private SensorEventListener listener = new SensorEventListener() {
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {}

		@Override
		public void onSensorChanged(SensorEvent event) {
			//Aktualisiere nur, wenn sie noch nicht schlaeft, sonst nicht
			if (Model.getInstance().getState() == State.Awake) {
				if(event.values.length > 0) {
					float valueLight = 0;
					valueLight = event.values[0];
					//Einfach als "Dunkel" deklariert, kann angepasst werden
					AbstractWish wish = Model.getInstance().getWishes().get(WishType.WishToSleep);

					if(valueLight <=5) {
						//Wenn Dunkel und Wunsch nach Schlaf existiert: Erf�lle Wunsch
						if(wish != null) {
							if (!wish.canBeFulfilled()) {
								makeSleepRoomToast("KEIN SAUBERES NACHTHEMND ODER KLEID!! WASCHE WAESCHE!!");
							} else {
								wish.fulfill();
								//Oma schlaeft!
								MainActivity.mainActivity.sleep();
								//Oma ins Bett legen
								sleep();
							}
						} 
						else {
							//Oma schlaeft!
							MainActivity.mainActivity.sleep();
							//Oma ins Bett legen
							((ImageView)findViewById(R.id.bedroom)).setImageResource(R.drawable.bedroom_disable);
							Model.getInstance().sleep(Model.SCHLAFEN_KURZ);
						}

					}
				}
			}
		}

	};

	public void startActivityGardrobe() {
		Intent intent = new Intent(this, GardrobeActivity.class);
		startActivity(intent);
	}
	
	//"schließen" Button
	public void onButtonClick(View view) {
		this.finish();
	}

	public void makeSleepRoomToast(String text) {
		Toast.makeText(
				this, 
				text + "", 
				Toast.LENGTH_SHORT)
				.show();
	}

	private BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();

			if(bundle.containsKey("wakeUp")) {
				if(bundle.getBoolean("wakeUp")) {
					wakeup();
				}
			}
		}
	};

	public void wakeup() {
		((ImageView)findViewById(R.id.bedroom)).setImageResource(R.drawable.bedroom);
	}
	public void sleep() {
		((ImageView)findViewById(R.id.bedroom)).setImageResource(R.drawable.bedroom_disable);
	}
	@Override
	protected void onResume() {
		super.onResume();
		Model.getInstance().inAppFocus = true;
		Log.i("Activity", "SleeproomActivity onResume()");
		getApplicationContext().registerReceiver(mReceiver, new IntentFilter(AliveService.ACTION_BROADCAST));
		if (Model.getInstance().getState() == State.Sleeping) {
			sleep();
		}
		else {
			wakeup();
		}
	}

	@Override
	protected void onStop() {
		getApplicationContext().unregisterReceiver(mReceiver); //unregister receiver
		super.onStop();
	}

	@Override
	protected void onPause() {
		super.onPause();
		Model.getInstance().inAppFocus = false;
		manager.unregisterListener(listener);
	}

	@Override
	protected void onStart() {
		super.onStart();
		sleepRoomActivity = this;
	}
}
